﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ERP.ERPModels.Production.MasterData;
using ERP.MasterData.Dtos;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.MasterData
{
    public class CustomerManagementService : ApplicationService, ICustomerManagementService
    {
        private readonly IRepository<Customers, int> _customerRepository;

        public CustomerManagementService(IRepository<Customers, int> customerRepository)
        {
            _customerRepository = customerRepository;
        }

        public async Task<CustomerManagementDto> CreateAsync(CustomerManagementDto input)
        {
            var entity = ObjectMapper.Map<CustomerManagementDto, Customers>(input);
            await _customerRepository.InsertAsync(entity);
            await CurrentUnitOfWork.SaveChangesAsync();
            return ObjectMapper.Map<Customers, CustomerManagementDto>(entity);
        }

        public async Task<CustomerManagementDto> UpdateAsync(int id, CustomerManagementDto input)
        {
            var entity = await _customerRepository.GetAsync(id);
            ObjectMapper.Map<CustomerManagementDto, Customers>(input, entity);
            await _customerRepository.UpdateAsync(entity);
            await CurrentUnitOfWork.SaveChangesAsync();
            return ObjectMapper.Map<Customers, CustomerManagementDto>(entity);
        }

        public async Task DeleteAsync(int id)
        {
            await _customerRepository.DeleteAsync(id);
        }

        public async Task BatchDeleteAsync(List<int> ids)
        {
            foreach (var id in ids)
            {
                await _customerRepository.DeleteAsync(id);
            }
        }
    }
}
